package com.jk.lesson4;

import com.jk.bean.Dish;
import com.jk.bean.DishType;

import java.util.*;
import java.util.stream.Collectors;

/**
 * Created by Auser on 2016/11/11.
 */
public class MenuTest {

    public static final List<Dish> menu =
            Arrays.asList( new Dish("pork", false, 800, DishType.MEAT),
                    new Dish("beef", false, 700, DishType.MEAT),
                    new Dish("chicken", false, 400, DishType.MEAT),
                    new Dish("french fries", true, 530, DishType.OTHER),
                    new Dish("rice", true, 350, DishType.OTHER),
                    new Dish("season fruit", true, 120, DishType.OTHER),
                    new Dish("pizza", true, 550, DishType.OTHER),
                    new Dish("prawns", false, 400, DishType.FISH),
                    new Dish("salmon", false, 450, DishType.FISH));

    public static void main(String...args){
        List<Dish> lowCaloricDishes = new ArrayList<>();
        for(Dish d : menu)
            if(d.getCalories() < 400)
                lowCaloricDishes.add(d);
        Collections.sort(lowCaloricDishes, new Comparator<Dish>() {
            @Override
            public int compare(Dish o1, Dish o2) {
                return Integer.compare(o1.getCalories(), o2.getCalories());
            }
        });

        List<String> lowCaloricDishesName = new ArrayList<>();
        for(Dish d : lowCaloricDishes)
            lowCaloricDishesName.add(d.getName());

        System.out.println(lowCaloricDishesName.toString());

        List<String> lowCaloricDishesName2 =
//                menu.stream()
                menu.parallelStream()
                        .filter(dish -> dish.getCalories() < 400)
                        .sorted(Comparator.comparing(Dish::getCalories))
                        .map(Dish::getName)
                        .collect(Collectors.toList());
        System.out.println(lowCaloricDishesName2.toString());

        Map<DishType, List<Dish>> dishesByType = menu.stream().collect(Collectors.groupingBy(Dish::getType));
        System.out.println(dishesByType.toString());

        menu.stream().forEach(System.out::println);

        long len = menu.stream().count();
        System.out.println(len);


    }
}
